/*
' Copyright (c) 2011 DotNetNuke Corporation
'  All rights reserved.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
'
*/

using System;
using System.Collections.Generic;
using System.Data;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;
using DotNetNuke.Modules._2DoTasks.Components;
using DotNetNuke.Modules._2DoTasks.Components.Entities;

namespace DotNetNuke.Modules._2DoTasks.Data
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// An abstract class for the data access layer
    /// </summary>
    /// -----------------------------------------------------------------------------
    public abstract class DataProvider
    {
        #region Shared/Static Methods

        private static DataProvider provider;

        // return the provider
        public static DataProvider Instance()
        {
            if (provider == null)
            {
                const string assembly = "DotNetNuke.Modules._2DoTasks.Data.SqlDataprovider,_2DoTasks";
                Type objectType = Type.GetType(assembly, true, true);

                provider = (DataProvider)Activator.CreateInstance(objectType);
                DataCache.SetCache(objectType.FullName, provider);
            }

            return provider;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Not returning class state information")]
        public static IDbConnection GetConnection()
        {
            const string providerType = "data";
            ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(providerType);

            Provider objProvider = ((Provider)_providerConfiguration.Providers[_providerConfiguration.DefaultProvider]);
            string _connectionString;
            if (!String.IsNullOrEmpty(objProvider.Attributes["connectionStringName"]) && !String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings[objProvider.Attributes["connectionStringName"]]))
            {
                _connectionString = System.Configuration.ConfigurationManager.AppSettings[objProvider.Attributes["connectionStringName"]];
            }
            else
            {
                _connectionString = objProvider.Attributes["connectionString"];
            }

            IDbConnection newConnection = new System.Data.SqlClient.SqlConnection();
            newConnection.ConnectionString = _connectionString.ToString();
            newConnection.Open();

            return newConnection;
        }

        #endregion Shared/Static Methods

        #region "Abstract Methods"

        #region "Task"

        public abstract int Add(_Task t);

        public abstract void Update(_Task t);

        public abstract void Delete(_Task t, int currentUserId);

        public abstract IDataReader GetTask(int id, int portalId, int currentUserId);

        public abstract IDataReader GetAllTasks(int portalId, int currentUserId);

        public abstract IDataReader FindTasks(
            int portalId, int currentUserId,
            SharedEnum.FilterAssigned assignedOptions,
            Nullable<SharedEnum.Task_FilterAditionalOptions> filterAditionalOptions, Nullable<int> projectId);

        public abstract void SetStatus(_Task t);

        #region "Comments"

        public abstract int Add(_TaskComment c, int portalId);

        public abstract void Update(_TaskComment c, int portalId);

        public abstract void Delete(_TaskComment c, int portalId, int currentUserId);

        public abstract IDataReader GetTaskComment(int id, int taskId, int currentUserId);

        public abstract IDataReader GetAllTaskComments(int taskId, int currentUserId);

        #endregion "Comments"

        #region "Time"

        public abstract int Add(_TaskTime t, int portalId);

        public abstract void Update(_TaskTime t, int portalId);

        public abstract void Delete(_TaskTime t, int portalId, int currentUserId);

        public abstract IDataReader GetTaskTime(int id, int taskId, int currentUserId);

        public abstract IDataReader GetAllTaskTimeEntries(int taskId, int currentUserId);

        #endregion "Time"

        #region "SubTask"

        public abstract int Add(_TaskSubTask t, int portalId);

        public abstract void Update(_TaskSubTask t, int portalId);

        public abstract void Delete(_TaskSubTask t, int portalId, int currentUserId);

        public abstract void UpdateOrder(_TaskSubTask t, int portalId, int newOrder);

        public abstract IDataReader GetTaskSubTask(int id, int taskId, int portalId, int currentUserId);

        public abstract IDataReader GetAllTaskSubTaskEntries(int taskId, int portalId, int currentUserId);

        #endregion "SubTask"

        #endregion "Task"

        #region "Project"

        public abstract int Add(_Project p);

        public abstract void Update(_Project p);

        public abstract void Delete(_Project p, int currentUserId);

        public abstract IDataReader GetProject(int id, int portalId, int currentUserId);

        public abstract IDataReader GetProject(string name, int portalId, int currentUserId);

        public abstract IDataReader GetProjectStartsWith(string name, int portalId, int currentUserId);

        public abstract IDataReader GetAllProjects(int portalId, int currentUserId);

        public abstract IDataReader GetArchived(int portalId, int currentUserId);

        #region "Project Members"

        public abstract void Add(_ProjectMember p, int currentUserId);

        public abstract void Update(_ProjectMember p, int currentUserId);

        public abstract void AddMultiple(List<_ProjectMember> p, int currentUserId);

        public abstract void Delete(_ProjectMember p, int currentUserId);

        public abstract void DeleteMultiple(List<_ProjectMember> p, int currentUserId);

        public abstract IDataReader GetProjectMember(int userId, int projectId, int currentUserId);

        public abstract IDataReader GetAllProjectMembers(int projectId, int currentUserId);

        public abstract Boolean IsProjectManager(int currentUserId, int projectId);

        #endregion "Project Members"

        #endregion "Project"

        #endregion "Abstract Methods"
    }
}